#ifndef DEF_CSOCKET
#define DEF_CSOCKET

#include <iostream>
#include <string>
#include <sstream>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>

class csocket {
	private:
		int c_socket;
		int c_buffer_size;
		bool c_stream;
	public:
		csocket(int domain = AF_INET, int type = SOCK_STREAM, int protocol = 0, int buffer_size = 1024);
		csocket(int socket, int buffer_size, bool stream);
		~csocket();
		//Connect the socket to a distant address/port
		bool cconnect(const struct sockaddr *my_addr, socklen_t addrlen);
		//Bind the socket to a local address/port
		bool cbind(const struct sockaddr *my_addr, socklen_t addrlen);
		//Make the socket listen, with the waitline length backlog
		bool clisten(int backlog);
		//Accept a waiting connection
		csocket *caccept(struct sockaddr *addr = NULL, socklen_t *addrlen = NULL);
		//Send data
		bool csend(const void *msg, size_t len, int flags = 0);
		bool csend(std::string msg, int flags = 0);
		//Receive data
		int creceive(std::string* message = NULL, int flags = 0);
		//Deactivate the socket (0 = send, 1 = receive, 2 = both)
		bool cshutdown(int how);
		//Close socket
		bool cclose();
		//Set/Get buffer length
		void cset_buffer_size(int buffer_size);
		int cget_buffer_size();
		//Setup a TCP server on all interfaces on the specified port
		//that accept the specified number of waiting connections
		bool cserver_TCP(int port, int backlog);
};

#endif
